home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
public
/
bit
/
src
/
clock_mail.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
9KB
|
446 lines
/*
* $Id: clock_mail.c,v 0.91 1994/02/20 00:53:31 zhao Pre-Release $
*
*. This file is part of BIT shareware package. After the two weeks of
* free evaluation period, you are encouraged (required) to register
* your copy for a small registration fee, which is $35 for personal use
* and $50 for commercial, government and institutional use.
*
* Copyright(c) 1993, 1994 by T.C. Zhao.
* All rights reserved.
*
* Permission to use, copy, and distribute this software in its entirety
* for non-commercial purposes is hereby granted, provided that the
* above shareware and copyright notices and this permission notice
* appear in all copies and their documentation.
*
* This software may be modified for your own use, but modified versions
* may not be distributed without prior consent of the author.
*
* This software is provided "as is" without expressed or implied
* warranty of any kind.
*
*.
*
* open a window and show a clock and a mailbox. Upon receiving ESC key, this
* process will die and optionally send iSIGUSR2 to its parant if whose pid
* is known. QKEY will simple quit CKEY coredump
*/
#ifndef lint
char *id = "$Id: clock_mail.c,v 0.91 1994/02/20 00:53:31 zhao Pre-Release $";
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include "gl/gl.h"
#include "gl/device.h"
#include <sys/stat.h>
#include <signal.h>
#include <errno.h>
#include "forms.h"
#include "bitmaps/newmail.xbm"
#include "bitmaps/nomail.xbm"
void gui_init(void);
#ifndef DEF_SLEEP
#define DEF_SLEEP 60
#endif
#ifndef LOCK_FILE
#define LOCK_FILE ".iclock.lc"
#endif
int interval = DEF_SLEEP;
struct stat buf;
static off_t size = 0;
static int fcol = 47; /* forground color */
static char *mailbox = 0;
static FL_FORM *clock_mail;
static FL_OBJECT *bitmap;
static long myqread(int, short *, long);
static void create_form_clock_mail(void);
static char *gcmd;
#define COLOR_NOMAIL 991
#define COLOR_MAIL 992
#define COLOR_BK 993
/* set default colors */
void
map_colors(void)
{
fl_init();
fl_mapcolor(COLOR_NOMAIL, 0, 0, 0);
fl_mapcolor(COLOR_MAIL, 0, 0, 255);
fl_mapcolor(COLOR_BK, 163, 163, 163);
fl_mapcolor(fcol, 163, 163, 163);
fl_mapcolor(0, 0, 0, 0);
}
/* show no mail bitmap */
void
nomail_bitmap(void)
{
fl_set_bitmap(bitmap, BM_X, BM_Y, nomail_bits);
fl_set_object_color(bitmap, COLOR_NOMAIL, COLOR_BK);
fl_redraw_object(bitmap);
}
/* show no mail bitmap */
void
newmail_bitmap(void)
{
fl_set_bitmap(bitmap, BM_Y, BM_Y, newmail_bits);
fl_set_object_color(bitmap, COLOR_MAIL, COLOR_BK);
ringbell();
fl_redraw_object(bitmap);
}
/* called to reset mailbox */
/* ARGSUSED */
void
reset_mail(FL_OBJECT * a, long ha)
{
size = stat(mailbox, &buf) ? 0 : buf.st_size;
nomail_bitmap();
}
/* see if new mail */
void
check_mail(void)
{
static int newmail = 0;
if (stat(mailbox, &buf))
{
newmail = 0;
reset_mail(0, 0);
return;
}
if (newmail)
{
if (buf.st_size < size)
{ /* other process read */
newmail = 0;
reset_mail(0, 0);
}
}
else
{
if (buf.st_size > size)
{ /* new mail arrived */
newmail = 1;
size = buf.st_size;
newmail_bitmap();
}
}
}
static int bitlaunched;
char lockfile[1024];
/* ARGSUSED */
void
finish_up(int sig)
{
if (bitlaunched && access(lockfile, F_OK) == 0)
remove(lockfile);
gexit();
exit(0);
}
/* check if there is lock file in existence. Need to do this
* because if it is bit launched, only can allow one copy to run
*/
void
check_status(void)
{
char *home, line[100];
FILE *fp;
int cid;
if (LOCK_FILE[0] != '/')
{
home = getenv("HOME");
if (!home)
{
fputs("Unable to find home\n", stderr);
exit(1);
}
strcpy(lockfile, home);
strcat(lockfile, "/");
strcat(lockfile, LOCK_FILE);
}
else
strcpy(lockfile, LOCK_FILE);
if (bitlaunched && access(lockfile, F_OK) == 0)
{
fp = fopen(lockfile, "r");
cid = 0;
if (fgets(line, 100, fp))
cid = atoi(line);
fclose(fp);
if (cid && kill(cid, 0) == 0)
{
fprintf(stderr, "%s is already runing with pid=%s", gcmd, line);
exit(0);
}
else if (!cid || errno == ESRCH)
{
remove(lockfile);
}
}
(void) signal(SIGUSR1, finish_up);
(void) signal(SIGTERM, finish_up);
}
/*********************************************************************/
char *optstr = "fbs:m:p:"; /* b undocumented */
char *optinfo = "%s [-f] [-s sec] [-m mbox] [x y [w h]]\n";
void
usage(char *c)
{
fprintf(stderr, optinfo, c);
exit(1);
}
static parent = -1;
int
main(int argc, char *argv[])
{
short val;
extern char *optarg;
extern optind, opterr;
char *qq;
int frame = 0, done = 0, c;
int x1 = -1, y1 = -1, w = 190, h = 50;
FILE *fp;
gcmd = argv[0];
opterr = 0;
while ((c = getopt(argc, argv, optstr)) != EOF)
{
switch (c)
{
case 'c':
fcol = atoi(optarg);
break;
case 's':
interval = atoi(optarg);
break;
case 'm':
mailbox = optarg;
break;
case 'p':
parent = atoi(optarg);
break;
case 'f':
frame = !frame;
break;
case 'b':
bitlaunched = 1;
break;
default:
usage(argv[0]);
break;
}
}
/*
* check_status simple checks to see if this program is already running,
* and quit if it is running AND is launched by bit
*/
check_status();
if (optind < argc)
{
x1 = atoi(argv[optind]);
optind++;
}
if (optind < argc)
{
y1 = atoi(argv[optind]);
optind++;
}
if (optind < argc)
{
w = atoi(argv[optind]);
optind++;
}
if (optind < argc)
{
h = atoi(argv[optind]);
optind++;
}
if (!mailbox)
mailbox = (qq = getenv("MAIL")) ? qq : getenv("MAILBOX");
/* check */
if (!mailbox)
{
/* make up one on the fly */
static char mbox[1024], *user;
strcpy(mbox, "/usr/mail/");
user = (qq = getlogin())? qq : getenv("USER");
if (!user)
{
fputs("Cant't fine mailbox\n", stderr);
finish_up(1);
}
mailbox = strcpy(mbox, user);
}
if (interval < 0)
interval = DEF_SLEEP;
create_form_clock_mail();
map_colors();
reset_mail(0, 0);
if (w > 0 && h > 0)
{
prefposition(x1, x1 + w - 1, y1, y1 + h - 1);
fl_show_form(clock_mail, FL_PLACE_FREE, frame, "clock & Mail");
}
else if (x1 > -100)
{
fl_set_form_position(clock_mail, x1, y1);
fl_show_form(clock_mail, FL_PLACE_POSITION, frame, "clock & Mail");
}
winset(clock_mail->window);
winpop();
fl_qdevice(ESCKEY);
fl_qdevice(QKEY);
fl_qdevice(RKEY);
fl_qdevice(CKEY);
fl_qreset();
if (bitlaunched)
{
if (!(fp = fopen(lockfile, "w")))
{
fprintf(stderr, "Can't create lock file\n");
finish_up(0);
}
fprintf(fp, "%d\n", getpid());
fclose(fp);
}
while (!done)
{
long dev;
check_mail();
done = 1;
if ((dev = myqread(interval, &val, 123456)) == ESCKEY && val)
{
if (parent > 0)
kill(parent, SIGUSR1);
}
else if (dev == RKEY && val)
{
if (parent > 0)
kill(parent, SIGUSR2);
}
else if (dev == CKEY && val)
{
if (parent > 0)
kill(parent, SIGABRT);
}
else if (dev == QKEY && val)
{
; /* what to do ? */
}
else
{
done = 0;
}
}
finish_up(0);
return 0;
}
/*
* myqreqd.c
*
* Read event queue within a time limit specified by seconds and mseconds.
* Default returned value is def. Nagative time disables time-out.
*/
#include <setjmp.h>
#include <signal.h>
static jmp_buf nokey;
static int catch;
static long returned;
/* sysV */
#define VOID void
#define VAL
static long
myqread(int seconds, short *val, long def)
{
VOID trapalarm(int);
VOID(*Oint) (int);
if (seconds <= 0)
return fl_qread(val);
*val = 1;
returned = def;
Oint = signal(SIGINT, trapalarm);
(void) signal(SIGALRM, trapalarm);
alarm((unsigned) seconds);
catch = 1;
if (setjmp(nokey) == 0)
{
fl_check_forms();
while (!fl_qtest())
fl_do_forms();
returned = fl_qread(val);
alarm(0);
}
else
{
catch = 0;
}
catch = 0;
(void) signal(SIGINT, Oint);
alarm(0);
return (returned);
}
/* ARGSUSED */
VOID
trapalarm(int sig)
{
alarm(0);
if (catch)
longjmp(nokey, 1);
return VAL;
}
static void
create_form_clock_mail(void)
{
FL_OBJECT *obj;
clock_mail = fl_bgn_form(FL_NO_BOX, 190.0, 85.0);
obj = fl_add_box(FL_UP_BOX, 0.0, 0.0, 190.0, 85.0, "");
obj = fl_add_clock(FL_SQUARE_CLOCK, 25.0, 10.0, 70.0, 65.0, "");
fl_set_object_boxtype(obj, FL_ROUNDED_BOX);
bitmap = fl_add_bitmap(FL_NORMAL_BITMAP, 105, 10, 70, 65, "");
obj = fl_add_button(FL_HIDDEN_BUTTON, 105.0, 10, 70.0, 65.0, "");
fl_set_call_back(obj, reset_mail, 0);
fl_end_form();
}